<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
 * 角色权限管理
 */
namespace app\admin\controller;

use  Mscore\Core\Controller;
use  app\admin\model\Smscode as SmscodeModel;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;


class SmscodeController extends Controller{

	  private $model;
    private $login;
    private $error=['code'=>0,'msg'=>'fail'];
    private $success=['code'=>200,'msg'=>'success']; 

    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->model = new SmscodeModel;
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }

    /**
     * 删除
     * @param array $del 
     * @param array $where 
     */ 
    public function del(){ 
         $info=$this->model->del(post('id',false));
         switch ($info) {
          case true:
            $this->echoJson($this->success);
            break;        
          default:
            $this->echoJson($this->error);
            break;
        }
    }
    
    /**
     * 单项列表
     * @param array $list 
     * @param array $where 
     */ 
   public function list(){

        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = ' 1>0 ';
            if(post('email')){
                $where .= ' and a.email = "'.post('email').'"';
            }
            if(post('mobile')){
                $where .= ' and a.mobile = "'.post('mobile').'"';
            }
            $datalist = $this->model->list($where,$page,$limit);
            foreach ($datalist['datalist'] as $key => $value) {
              $datalist['datalist'][$key]['endtime']=date("Y-m-d H:i:s", $value['endtime']);
            }
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echoJson($data);
        } else {
            $this->display('list.html');
        }
    }
    
}
